<template>
  <div class="app-container">
    <el-card class="content-edit">
      <div slot="header">添加轮播图</div>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form['title']"/>
        </el-form-item>
        <el-form-item label="图片" prop="icon">
          <div class="image-box">
            <ws-upload-single-img v-model="form['icon']" :path="form['iconUrl']" @finish="getUrl"/>
            <div class="image-remark">
              <div class="remarks"><span class="symbol">⁎</span>注：1. 图片单张大小支持1M一下,超过系统会自动压缩</div>
              <div class="remarks">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2. 推荐上传分辨率为675px*120px的图片</div>
              <div class="remarks">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3. 支持的图片格式 .jpg、.jpeg、.png、</div>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="跳转路径">
          <el-select v-model="form['url']" placeholder="请选择页面路径" clearable class="page-select">
            <el-option v-for="item in pagePathList" :key="item.url" :label="item['title']" :value="item['url']"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading" type="primary" @click="onSubmit">提交</el-button>
          <el-button @click="$router.back()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
  import checkImageSize from '@/utils/index'
  export default {
    name: 'HomepageBannerAdd',
    data() {
      return {
        loading: false,
        // 页面路径列表
        pagePathList: [],
        form: {
          // 图片标题
          title: '',
          //  跳转路径
          url: null,
          // 图片
          icon: '',
          // 图片地址
          iconUrl: ''
        },
        rules: {
          title: [{ required: true, message: '标题不为空', trigger: 'change' }],
          icon: [{ required: true, message: '请选择图片', trigger: 'change' }]
        }
      }
    },
    mounted(){
      this.loadPagePathList();
    },
    methods: {checkImageSize,
      // 获取选择的图片真实地址
      getUrl(e){
        console.log(e);
        this.form['iconUrl'] = e;
      },
      // 加载小程序页面路径列表
      loadPagePathList(){
        (async () => {
          const result = await this.$service.applicationGetWxAppPagePath();
          if (result.code === 0) {
            this.pagePathList = result.data;
          }
        })();
      },
      // 提交
      onSubmit() {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.loading = true;
            (async () => {
              const params = Object.assign({}, this.form);
              const check = await this.checkImageSize(params['iconUrl'], 675, 120)
              if (!check) {
                this.$message.error('图片尺寸不符合规范~');
                this.loading = false;
                return false;
              }
              params.imageSize = check;
              for (let i = 0; i < this.pagePathList.length; i++) {
                if (params.url === this.pagePathList[i].url) {
                  params.pageName = this.pagePathList[i].title;
                  break;
                }
              }
              const result = await this.$service.applicationAddBanner(params);
              // const result = await this.$service.applicationToRequestWxCloudApi({
              //   action: 'addBanner',
              //   params: JSON.stringify(params)
              // });
              this.loading = false;
              if (result.code === 0) {
                this.$message.success('操作成功~');
                this.$router.back();
              } else {
                this.$message.error(result.msg);
              }
            })();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }
</script>

<style scoped>
  .page-select {
    width: 65%;
  }
</style>

